package com.example.framework.helper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;
import androidx.loader.content.CursorLoader;

import com.example.framework.utils.LogUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author:0.2℃
 * @date： 2022-04-04 20:45
 * @Description ：关于文件的帮助类
 */
public class FileHelper {
    // 相机
    public static final int CAMERA_REQUEST_CODE = 1004;
    // 相册
    public static final int ALBUM_REQUEST_CODE = 1005;

    private static volatile FileHelper instance = null;

    private File tempFile = null;
    private Uri imgUri;

    private SimpleDateFormat simpleDateFormat;

    private FileHelper() {
        simpleDateFormat = new SimpleDateFormat("yyy-MM-dd-HH-mm-ss");
    }

    public static FileHelper getInstance() {
        if (instance == null) {
            synchronized (FileHelper.class) {
                if (instance == null) {
                    instance = new FileHelper();
                }
            }
        }
        return instance;
    }

    public File getTempFile() {
        return tempFile;
    }

    /**
     * 相机
     * @param mActivity
     */
    public void toCamera(Activity mActivity) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String fileName = simpleDateFormat.format(new Date());
        tempFile = new File(Environment.getExternalStorageDirectory(), fileName + ".jpg");
        // 兼容Android N（7）
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            // Android版本低于7，通过这个方式获得照片的真实地址
            imgUri = Uri.fromFile(tempFile);
        } else {
            // 使用FileProvider
            imgUri = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + ".fileprovider", tempFile);
            // 添加权限
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        LogUtils.i("imgUri :" + imgUri);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        mActivity.startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }

    /**
     * 跳转到相册
     * @param mActivity
     */
    public void toAlbum(Activity mActivity) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        mActivity.startActivityForResult(intent, ALBUM_REQUEST_CODE);
    }

    /**
     * 通过uri去系统查询真实地址
     * @param mContext
     * @param uri
     */
    public String getReadPathFromURI(Context mContext, Uri uri) {
        // 这是查询的条件
        String[] proj = {MediaStore.Images.Media.DATA};
        CursorLoader cursorLoader = new CursorLoader(mContext, uri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        int index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(index);
    }

}
